package com.anfeng.cms.service.impl.emp;

import com.anfeng.cms.common.Common;
import com.anfeng.cms.domain.emp.AfEmployeePhoto;
import com.anfeng.cms.repository.mysql.dao.emp.AfEmployeePhotoMapper;
import com.anfeng.cms.request.emp.EmployeePhotoDTO;
import com.anfeng.cms.service.emp.IAfEmployeePhotoService;
import com.anfeng.cms.service.emp.UploadImageService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴朋
 * @since 2019-07-15
 */
@Service
@Slf4j
public class AfEmployeePhotoServiceImpl extends ServiceImpl<AfEmployeePhotoMapper, AfEmployeePhoto> implements IAfEmployeePhotoService {

    @Autowired
    private UploadImageService uploadImageService;

    /**
     * 根据员工id 获取员工地址
     *
     * @param empId 员工id
     * @return 图片地址信息
     */
    @Override
    public List<EmployeePhotoDTO> getEmployeePhotos(String empId) {
        if (StringUtils.isBlank(empId)) {
            log.error("根据员工id 获取员工图片地址 失败 传入参数为nul");
            return Collections.EMPTY_LIST;
        }
        AfEmployeePhoto employeePhoto = new AfEmployeePhoto();
        employeePhoto.setEmpId(empId);
        employeePhoto.setState(Common.DATA_STATE_NORMAL);
        EntityWrapper<AfEmployeePhoto> wraper = new EntityWrapper<>();
        wraper.setEntity(employeePhoto);
        wraper.orderBy("create_time", false);

        List<AfEmployeePhoto> employeePhotos = this.selectList(wraper);
        if (null == employeePhotos || employeePhotos.isEmpty()) {
            return Collections.EMPTY_LIST;
        }
        return employeePhotos.stream().map(afEmployeePhoto -> {
            EmployeePhotoDTO dto = new EmployeePhotoDTO();
            dto.setId(afEmployeePhoto.getId());
            dto.setEmpId(afEmployeePhoto.getEmpId());
            dto.setCreateTime(afEmployeePhoto.getCreateTime());
            dto.setName(afEmployeePhoto.getName());
            dto.setPath(afEmployeePhoto.getPath());
            dto.setUrl(uploadImageService.getImageUrlById(afEmployeePhoto.getId()));
            return dto;
        }).collect(Collectors.toList());
    }
}
